package com.ruoyi.web.controller.resource;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.resource.domain.ResInfomation;
import com.ruoyi.resource.service.IResInfomationService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 资讯Controller
 * 
 * @author ruoyi
 * @date 2021-12-20
 */
@RestController
@RequestMapping("/resource/infomation")
public class ResInfomationController extends BaseController
{
    @Autowired
    private IResInfomationService resInfomationService;

    /**
     * 查询资讯列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ResInfomation resInfomation)
    {
        startPage();
        List<ResInfomation> list = resInfomationService.selectResInfomationList(resInfomation);
        return getDataTable(list);
    }

    /**
     * 查询资讯列表
     */
    @GetMapping("/hottext")
    public TableDataInfo hottext(ResInfomation resInfomation)
    {
        List<ResInfomation> list = resInfomationService.selectResInfomationHotList(resInfomation);
        return getDataTable(list);
    }

    /**
     * 查询好友动态资讯列表
     */
    @GetMapping("/feedList")
    public TableDataInfo feedList(ResInfomation resInfomation)
    {
        resInfomation.setUserId(getUserId());
        List<ResInfomation> list = resInfomationService.selectResInfomationNewsFeedList(resInfomation);
        return getDataTable(list);
    }
    /**
     * 导出资讯列表
     */
    @PreAuthorize("@ss.hasPermi('resource:infomation:export')")
    @Log(title = "资讯", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ResInfomation resInfomation)
    {
        List<ResInfomation> list = resInfomationService.selectResInfomationList(resInfomation);
        ExcelUtil<ResInfomation> util = new ExcelUtil<ResInfomation>(ResInfomation.class);
        util.exportExcel(response, list, "资讯数据");
    }

    /**
     * 获取资讯详细信息
     */
    @GetMapping("/getInfo")
    public TableDataInfo getInfo(ResInfomation resInfomation)
    {
        resInfomation.setUserId(getUserId());
        List<ResInfomation> list = resInfomationService.selectResInfomationById(resInfomation);
        return getDataTable(list);
    }

    /**
     * 新增资讯
     */
    @Log(title = "资讯", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ResInfomation resInfomation)
    {
        resInfomation.setCreateBy(getUsername());
        resInfomation.setUserId(getUserId());
        return toAjax(resInfomationService.insertResInfomation(resInfomation));
    }

    /**
     * 修改资讯
     */
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ResInfomation resInfomation)
    {
        return toAjax(resInfomationService.updateResInfomation(resInfomation));
    }

    /**
     * 修改资讯点赞
     */
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping("/calcPraise")
    public AjaxResult editPraise(@RequestBody ResInfomation resInfomation)
    {
        return toAjax(resInfomationService.updateResInfomationPraise(resInfomation.getId()));
    }
    /**
     * 修改资讯收藏
     */
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping("/calcCollect")
    public AjaxResult editCollect(@RequestBody ResInfomation resInfomation)
    {
        return toAjax(resInfomationService.updateResInfomationCollect(resInfomation.getId()));
    }

    /**
     * 修改资讯分享
     */
    @Log(title = "资讯", businessType = BusinessType.UPDATE)
    @PutMapping("/calcShare")
    public AjaxResult editShare(@RequestBody ResInfomation resInfomation)
    {
        return toAjax(resInfomationService.updateResInfomationShare(resInfomation.getId()));
    }

    /**
     * 删除资讯
     */
    @Log(title = "资讯", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(resInfomationService.deleteResInfomationByIds(ids));
    }
}
